<template>
  <div>
    <!--  @click-right="onClickRight" -->
    <van-nav-bar title="编辑昵称" left-text="取消" right-text="确定" left-arrow @click-left="$emit('close-popup')"
      @click-right="saveInfo" />

    <van-field v-model="name" type="textarea" placeholder="请输入新昵称" maxlength="8" show-word-limit />
  </div>
</template>

<script>
import { editUserInfo } from '@/api/user.js'
export default {
  name: 'EditName',
  data () {
    return {
      name: this.value
    }
  },
  props: {
    value: {
      type: String,
      required: true
    }
  },
  methods: {
    // 点击保存修改用户信息
    async saveInfo () {
      // 对表单进行校验
      if (this.name.trim() === '') return this.$toast('请输入要修改的昵称')
      // 自定义加载图标
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0
      })
      try {
        await editUserInfo({
          name: this.name
        })
        this.$toast.success('修改成功')
        this.$emit('close-popup')
        this.$emit('input', this.name)
      } catch (err) {
        if (err.response && err.response.status === 409) {
          return this.$toast.fail('亲，用户名已存在')
        }
        this.$toast.fail('修改失败')
      }
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () { },
  mounted () { }
}
</script>

<style scoped lang='less'>
</style>
